03. Working with documents

JavaND#305 C03 L03 A03 Create Documents

Exercise

Task Description:

Environment Setup

MongoDB server is started.
Use mongo shell to connect to your local MongoDB server.
Select the database jdnd-c3.

Document Structure

The member document represents the members of a health club.

{
    "_id": “cjenkins”,
    "first_name": "Carl",
    "last_name": "Jenkins",
    "gender": "male",
    "age": 23,
    "address": {
        "street": "123 Main Street",
        "city": "Oakland",
        "state": "CA"
    },
    "interests": ["pilates","swim","crossfit"],
    "balance": 125.20
}
Task List:

Task Feedback:

Well done! You now know how to create,update and delete documents in a collection.

JavaND#305 C03 L03 A04 Querying Documents

Exercise

Task Description:

Environment Setup

Use mongo shell to connect to your local MongoDB server.
Select the database jdnd-c3.
Run db.members.remove({}) to remove all documents in the collection.
Insert seed data given below.

Seed Data

db.members.insert([{
  first_name: "Jane",
  last_name: "Doe",
  age: 45,
  gender: "female",
  interests: ["pilates","swim","crossfit"],
  balance: 125.20,
  address: {
      street: "123 Main St",
      city: "Birmingham",
      state: "AL"
  }
},
{
  first_name: "John",
  last_name: "Doe",
  age: 47,
  gender: "male",
  interests: ["pilates","swim"],
  balance: 56.25,
  address: {
      street: "123 First St",
      city: "Minneapolis",
      state: "MN"
  }
},
{
  first_name: "Lakshmi",
  last_name: "Natarajan",
  age: 29,
  gender: "female",
  interests: ["swim", "tennis"],
  balance: 556.39,
  address: {
      street: "149 Main St",
      city: "Birmingham",
      state: "AL"
  }
},
{
  first_name: "Eduardo",
  last_name: "Lopez",
  age: 32,
  gender: "male",
  interests: ["swim", "tennis", "golf"],
  balance: 1034.23,
  address: {
      street: "298 Second St",
      city: "Birmingham",
      state: "AL"
  }
},
{
  first_name: "Sana",
  last_name: "Khan",
  age: 53,
  gender: "female",
  interests: ["golf"],
  balance: 500,
  address: {
      street: "649 First St",
      city: "Minneapolis",
      state: "MN"
  }
},
{
  first_name: "Rahul",
  last_name: "Mani",
  age: 18,
  gender: "male",
  interests: ["swim"],
  balance: 250,  
  address: {
      street: "41 Second Ave",
      city: "Indianapolis",
      state: "IN"
  }
}
]);
Task List:

Task Feedback:

Well done! You used the various query methods in MongoDB.